package com.yang.orderconsumer.controller;

import com.alibaba.fastjson.JSONObject;
import com.yang.springcloud.pojo.Order;
import com.yang.springcloud.pojo.ShoppingCartGoods;
import com.yang.springcloud.service.OrderClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
public class OrderController {
    @Autowired
    private OrderClientService orderClientService;

    @PostMapping("/orderinsert")
    public Map<String, Object> addStudent(@RequestParam(name = "useridstr") String useridstr, @RequestParam(name = "username") String username, @RequestParam(name = "goodListStr") String goodListStr) {
        System.out.println(useridstr+ " " + username + " " + goodListStr);

        int userid = Integer.parseInt(useridstr);
        Order o = new Order();
        o.setOrderUserId(userid);
        o.setOrderUserName(username);
        o.setGoodList(goodListStr);

        o.setGoodListStruct(JSONObject.parseArray(goodListStr, ShoppingCartGoods.class));

        return orderClientService.insertOrder(o);
    }
}
